package com.ysx.oa.user.controller;

import com.ysx.oa.role.bean.UserRoleBean;
import com.ysx.oa.student.bean.StudentBean;
import com.ysx.oa.user.bean.UserBean;
import com.ysx.oa.user.dao.UserDao;
import com.ysx.oa.user.dto.UserDto;
import com.ysx.oa.user.service.UserService;
import com.ysx.oa.user.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 周海燕
 * @Date: 2021/11/21 16:19
 */


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserDao userDao;


    /**
     * 方法描述:http://localhost:8080/kaoqin/user/register
     * @author: 周海燕
     * @date: 2021/11/25 14:57 [userBean] java.util.Map<java.lang.String,java.lang.Object>
     * @CrossOrigin；解决跨域
     */
    @CrossOrigin
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Map<String, Object> register(@RequestBody UserBean userBean, HttpServletRequest request) {
        Integer row = userService.register(userBean);

        HttpSession session = request.getSession(true);
        session.setAttribute("phone",userBean.getPhone());

        Map<String, Object> map = new HashMap<>();
        if (row != 0) {
            map.put("code", 200);
            map.put("data",session.getAttribute("phone"));
            map.put("message", "注册成功");

        } else {
            map.put("code", 691);
            map.put("message", "注册失败");
            map.put("message1", "用户账号已存在或账号格式输入错误");
        }
        return map;
    }


    /**
     * 方法描述:http://localhost:8080/kaoqin/user/login
     * @author: 周海燕
     * @date: 2021/11/25 14:58 [userBean, request] java.util.Map<java.lang.String,java.lang.Object>
     * @CrossOrigin；解决跨域
     */
    @CrossOrigin
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Map<String, Object> login(@RequestBody UserBean userBean, HttpServletRequest request) {

        UserRoleBean user = userService.login(userBean);

        // 开启session
        HttpSession session = request.getSession(true);
        System.out.println(session.getId());
        session.setAttribute("userId", user.getUserId());
        session.setAttribute("roleId",user.getRoleId());



        Map<String, Object> map = new HashMap<>();
        if (user.getUserId() == 1) {
            map.put("code", 403);
            map.put("data", user.getUserId());
            map.put("message", "登录成功，暂无权限，待系统管理员审核");
        } else if (user.getUserId() == 0) {
            map.put("code", 691);
            map.put("message", "您输入的账号或密码不正确");
        } else {
            map.put("code", 200);
            map.put("data", user.getRoleId());
            map.put("message", "登陆成功");
        }
        return map;
    }



    /**
     * 方法描述:登录加载时，返回phone
     * 实现功能：注册时的手机号码注册成功，跳转登录界面，自动将手机号填入
     * @author: 周海燕
     * @date: 2021/11/27 18:16 [request] java.util.Map<java.lang.String,java.lang.Object>
     */
    // http://localhost:8080/kaoqin/user/loginLoading
    // 解决跨域
    @CrossOrigin
    @RequestMapping(value = "/loginLoading", method = RequestMethod.GET)
    public Map<String, Object> loginLoading(HttpServletRequest request){
        HttpSession session = request.getSession(true);
        Object phone = session.getAttribute("phone");

        Map<String, Object> map = new HashMap<>();

        map.put("code", 200);
        map.put("data", phone);

        return map;
    }

    /**
     * 方法描述:http://localhost:8080/kaoqin/user/findUserByUserId/rest/100006
     * 前端拿值：id，phone，name
     * @author: 周海燕
     * @date: 2021/11/25 14:59 [userId]
     * @CrossOrigin；解决跨域
     */
    @CrossOrigin
    @RequestMapping(value = "/findUserByUserId/rest/{userId}", method = RequestMethod.GET)
    public Map<String, Object> findUserByUserId(@PathVariable("userId") Integer userId) {
        UserBean user = userService.findUserByUserId(userId);

        Map<String, Object> map = new HashMap<>();

        if(user != null){
            map.put("code", 200);
            map.put("data", user);
        }else{
            map.put("code", 500);
            map.put("message", "fail");
        }
        return map;
    }


    /**
     * 方法描述:http://localhost:8080/kaoqin/user/updateUserInfo
     * @author: 周海燕
     * @date: 2021/11/25 15:12 [userBean] java.util.Map<java.lang.String,java.lang.Object>
     * 前端：gender,mail,education,whetherEmploy
     */
    @CrossOrigin
    @RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST)
    public Map<String, Object> updateUserInfo(@RequestBody UserDto userDto,HttpServletRequest request) {
        HttpSession session = request.getSession(true);
        Object userId = session.getAttribute("userId");
        Object roleId = session.getAttribute("roleId");

        userDto.setId((Integer) userId);
        userDto.setRoleId((Integer) roleId);

        UserDto UserInfo = userService.updateUserInfo(userDto);

        Map<String, Object> map = new HashMap<>();

        if(UserInfo != null){
            map.put("code", 200);
            map.put("data", UserInfo);
        }else{
            map.put("code", 500);
            map.put("message", "fail");
        }
        return map;
    }
    // http://localhost:8080/kaoqin/user/findAllUserOrderByLastUpdateTime
    // 解决跨域

    @CrossOrigin
    @RequestMapping(value = "/findAllUserOrderByLastUpdateTime", method = RequestMethod.GET)
    public Map<String, Object> findAllUserOrderByLastUpdateTime() {
        Map<String, Object> map = new HashMap<>();

        List<UserBean> selectAllUser = userService.findAllUserOrderByLastUpdateTime();

        map.put("code", 200);
        map.put("data", selectAllUser);

        return map;
    }





    // http://localhost:8080/kaoqin/user/delete.do
    // 解决跨域
    @CrossOrigin
    @RequestMapping(value = "/delete.do", method = RequestMethod.POST)
    public Map<String, Object> delete(@RequestBody UserBean userBean) {
        Integer delete = userService.delete(userBean);

        Map<String, Object> map = new HashMap<>();

        map.put("code", 200);
        map.put("data", delete);

        return map;
    }


    // http://localhost:8080/kaoqin/user/find.do
    // 解决跨域
    @CrossOrigin
    @RequestMapping(value = "/find.do", method = RequestMethod.GET)
    public Map<String, Object> find() {
        Map<String, Object> map = new HashMap<>();

        List<UserBean> select = userService.selectAll();

        map.put("code", 200);
        map.put("data", select);

        return map;
    }



}
